<template>
  <a-modal
    title="新增产品类型表"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="名称"
              :labelCol="labelCol_T"
              :wrapperCol="wrapperCol_T"
              has-feedback
            >
              <a-input placeholder="请输入名称" v-decorator="['name', {rules: [{required: true, message: '请输入名称！'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="父级"
              :labelCol="labelCol_T"
              :wrapperCol="wrapperCol_T"
              has-feedback
            >
              <a-tree-select
                v-decorator="['pid', {initialValue: selProTypeId, rules: [{ required: true, message: '请选择父级！' }]}]"
                style="width: 100%"
                :dropdownStyle="{ maxHeight: '300px', overflow: 'auto' }"
                :treeData="proTree"
                treeDefaultExpandAll
                show-search
                tree-node-filter-prop="title"
                placeholder="请选择父级"
                allowClear
              >
                <span slot="title" slot-scope="{ title }">
                  {{ title }}
                </span>
              </a-tree-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              :labelCol="labelCol_T"
              :wrapperCol="wrapperCol_T"
              label="排序"
              has-feedback
            >
              <a-input-number placeholder="请输入排序" style="width: 100%" v-decorator="['sort', { initialValue: 100 }]" :min="1" :max="1000" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-form-item v-if="jsonAddShow">
          <json-add
            ref="jsonAdd"
            :fieldProp="fieldProp"
          />
        </a-form-item>
        <a-form-item
          label="备注"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-textarea placeholder="请输入备注" v-decorator="['remarks']" :auto-size="{minRows:3,maxRows:6}"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { proTypeAdd, getProTree } from '@/api/modular/main/protype/proTypeManage'
  import XEUtils from 'xe-utils'
  import JsonAdd from '@/components/MyComp/JsonAdd'
  import { fieldConfigList } from '@/api/modular/main/fieldconfig/fieldConfigManage'
  export default {
    components: {
      JsonAdd
    },
    props: {
      selProTypeId: {
        default: '0',
        type: String,
        required: false
      }
    },

    data () {
      return {
        fieldProp: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 3 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 20 }
        },
        labelCol_T: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol_T: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        jsonAddShow: true,
        proTree: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      add (record) {
        this.visible = true
        this.getProTree()
        this.dynamicFieldConfig()
      },
      // 动态字段赋值
      dynamicFieldConfig() {
        this.jsonAddShow = false
        this.fieldProp = {}
        fieldConfigList({ tableName: 'dw_pro_type' }).then(res => {
          res.data.forEach(item => {
            // 查询自定义字段
            this.fieldProp[item.fieldIndex] = {
              type: item.fieldType,
              title: item.fieldTitle,
              'x-decorator': 'FormItem',
              // 'x-component': 'a-input',
              'x-decorator-props': {
                labelCol: 6,
                // labelWrap: true,
                wrapperCol: 16
              },
              'x-component-props': {
                placeholder: '请输入',
                style: {
                  width: '100%'
                }
              }
            }
          })
          this.jsonAddShow = true
        })
      },
      /**
       * 获取类型树，并加载于表单中
       */
      getProTree () {
        getProTree().then((res) => {
          this.formLoading = false
          if (!res.success) {
            this.proTree = []
            return
          }
          this.proTree = [{
            'id': '-1',
            'parentId': '0',
            'title': '顶级',
            'value': '0',
            'pid': '0',
            'children': res.data
          }]
        })
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            values.name = XEUtils.trim(values.name)
            values.code = XEUtils.trim(values.code)
            values.remark = XEUtils.trim(values.remark)
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            this.$refs.jsonAdd.getAddForm().submit((josn) => {
              values.josn = josn
              proTypeAdd(values).then((res) => {
                if (res.success) {
                  this.$message.success('新增成功')
                  this.confirmLoading = false
                  this.$emit('ok', values)
                  this.handleCancel()
                } else {
                  this.$message.error(res.message)// + res.message
                }
              }).finally((res) => {
                this.confirmLoading = false
              })
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.$refs.jsonAdd.getAddForm().reset()
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
